package com.tenement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.entity.Idle;
import com.tenement.mapper.IdleMapper;
import com.tenement.service.IdleService;
import com.tenement.vo.QueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.tenement.entity.table.IdleTableDef.IDLE;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class IdleServiceImpl extends ServiceImpl<IdleMapper, Idle> implements IdleService {

    private final IdleMapper idleMapper;

    @Override
    public Page<Idle> list(QueryVo queryVo) {
        QueryWrapper wrapper = QueryWrapper.create().from(IDLE).where(
                IDLE.ADDRESS.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))
                        .or(IDLE.DESCRIBE.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
        ).and(IDLE.TYPE.eq(queryVo.getType(), queryVo.getType() != -1));
        if (queryVo.getAll() != -1) {
           return TenantManager.withoutTenantCondition(() -> idleMapper.paginate(queryVo.getPage(),queryVo.getSize(),wrapper));
        }
        return idleMapper.paginate(queryVo.getPage(),queryVo.getSize(),wrapper);
    }

}
